<?php

namespace app\api\controller\mox;

use app\common\controller\Api;
use app\admin\model\mox\play\Type as TypeModel;
use  app\admin\model\mox\play\Info as InfoModel;
use  app\admin\model\mox\play\Msg as MsgModel;

/**
 * 剧目
 */
class Play extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 列表
     *
     */
    public function list()
    {

        $type_id = $this->request->param('type_id');
        $search = $this->request->param('title');
        $condition = $this->request->param('condition');
        $where = [];
        if ($type_id) {
            $where['type_id'] = $type_id;
        }
        if ($search) {
            $where['title'] = ['like', '%' . $search . '%'];
        }
        if ($condition == 'represent') {
            $where['is_represent'] = 1;
        } elseif ($condition == 'perform') {
            $where['is_perform'] = 1;
        }
        $model = new InfoModel();
        $list = $model
            ->where($where)
            ->order('id desc')
            ->paginate()
            ->each(function ($item, $key) {
                $item['user'] = model('app\common\model\User')->where('id', $item['user_id'])->field('id, nickname, avatar')->find();
                $item['type'] = model('app\admin\model\mox\play\Type')->where('id', $item['type_id'])->field('id, title,image')->find();
            });
        $this->success('请求成功', $list);
    }
    /**
     * 类别
     */
    public function type()
    {
        $type_id = $this->request->param('type_id');
        $model = new TypeModel();
        if ($type_id) {
            $row = $model->where('id', $type_id)->field('id, title,image')->find();
            $this->success('请求成功', $row);
        } else {
            $list = $model->order('id desc')->select();
            $this->success('请求成功', $list);
        }
    }
    /**
     * 详情
     */
    public function detail()
    {
        $id = $this->request->param('id');
        if (!$id) {
            $this->error('缺少参数id');
        }
        $model = new InfoModel();
        $info = $model
            ->with(['user', 'type', 'videofilename'])
            ->where($model->getTable() . '.id', $id)
            ->find();
        if (!$info) {
            $this->error('剧目不存在');
        }
        // 相关角色
        $relatedpersons = explode(',', $info['related_persons']);
        $info['relatedpersons'] = model('app\admin\model\mox\inherit\Character')->where('id', 'in', $relatedpersons)->field('id, name, image')->select();
        // 相关章节
        $info['story'] = model('app\admin\model\mox\inherit\story\Info')->where('id', $info['s_id'])->find();
        if ($info['story']) {
            $info['chapter'] = model('app\admin\model\mox\inherit\story\Chapter')->where('id', $info['c_id'])->field('id, title')->find();
            $label_ids = explode(',', $info['story']['label_id']);
            $info['story']['label'] = model('app\admin\model\mox\inherit\story\Label')->where('id', 'in', $label_ids)->field('id, title,color')->select();
        }
        // 评论数
        $info['msg_num'] = model('app\admin\model\mox\play\Msg')->where('p_id', $info['id'])->count();
        //弹幕列表
        $info['msg'] = model('app\admin\model\mox\play\Msg')->where('p_id', $info['id'])->order('id desc')->limit(50)->select();
        $data = [];
        foreach ($info['msg'] as $key => $value) {
            $e = [
                'text' => $value['content'],
                'color' => '#ff0000',
                'time' => $value['second'],
            ];
            $data[] = $e;
        }
        $info['msg'] = $data;
        $this->success('请求成功', $info);
    }

    /**
     * 添加弹幕
     */
    public function adddMsg()
    {
        $params = $this->request->param();
        if ($params) {
            $params['createtime'] = time();
            $params['status'] = 0;
            $model = new MsgModel();
            $retunts = $model->save($params);
            $this->success('添加成功', $retunts);
        } else {
            $this->error('添加失败');
        }
    }
    /**
     * 弹幕列表
     */
    public function msgList()
    {
        $p_id = $this->request->param('id');
        if (!$p_id) {
            $this->error('缺少参数p_id');
        }
        $model = new MsgModel();
        $list = $model
            ->with(['user'])
            ->where('p_id', $p_id)
            ->order('id desc')
            ->paginate();


        $this->success('请求成功', $list);
    }
}
